package com.woniuxy.enjoylife.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.enjoylife.entity.Suggest;
import com.woniuxy.enjoylife.entity.dto.SuggestDto;
import com.woniuxy.enjoylife.mapper.CommunityMapper;
import com.woniuxy.enjoylife.mapper.SuggestMapper;
import com.woniuxy.enjoylife.service.SuggestService;
import com.woniuxy.enjoylife.utils.PageResult;
import com.woniuxy.enjoylife.utils.PageUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
public class SuggestServiceImpl extends ServiceImpl<SuggestMapper, Suggest> implements SuggestService {

    @Resource
    private SuggestMapper suggestMapper;
    @Resource
    private CommunityMapper communityMapper;

    //全查询投诉，包括用户，小区信息（分页）
    @Override
    public PageResult<List<SuggestDto>> findAll(int reuid,int current,int size) {
        //获取分页对象
        PageHelper.startPage(current,size);
        List<SuggestDto> list = suggestMapper.findAll(reuid);
        PageInfo<SuggestDto> pageInfo = new PageInfo<>(list);
        PageResult pageResult = PageUtil.setPageResult2(pageInfo);
        return pageResult;
    }

    //根据uid进行回复，并添加回复人id
    @Override
    public boolean reply(int id, String reply) {
        //根据uid修改
        Suggest suggest = new Suggest().setReply(reply).setReplyTime(LocalDateTime.now()).setResult("已处理");
        UpdateWrapper<Suggest> wrapper = new UpdateWrapper<Suggest>().eq("id",id);
        return suggestMapper.update(suggest,wrapper) > 0 ? true : false;
    }

    //根据内容模糊查询并分页
    @Override
    public PageResult<List<SuggestDto>> findLike(int reuid,String content,int current, int size) {
        //获取分页对象
        PageHelper.startPage(current,size);
        List<SuggestDto> list = suggestMapper.findLike(reuid, content);
        PageInfo<SuggestDto> pageInfo = new PageInfo<>(list);
        PageResult pageResult = PageUtil.setPageResult2(pageInfo);
        return pageResult;
    }

    //根据投诉id查询投诉，包括用户，小区信息
    @Override
    public SuggestDto findSuggestDtoById(int id) {
        return suggestMapper.findSuggestDtoById(id);
    }

    //根据投诉id删除（将status改为1）
    @Override
    public boolean del(int id) {
        Suggest suggest = new Suggest().setId(id).setStatus(1);
        return suggestMapper.updateById(suggest) > 0 ? true : false;
    }

    //**********************用户角度************************


    /**
     * 用户查询自己相关的投诉信息
     * @param uid
     * @param current
     * @param size
     * @return
     */
    @Override
    public PageResult<List<SuggestDto>> findUserSuggest(int uid,int current,int size) {
        //获取分页对象
        PageHelper.startPage(current,size);
        List<SuggestDto> list = suggestMapper.findUserSuggest(uid);
        PageInfo<SuggestDto> pageInfo = new PageInfo<>(list);
        PageResult pageResult = PageUtil.setPageResult2(pageInfo);
        return pageResult;
    }

    /**
     * 用户新增投诉
     * @param uid  用户id
     * @param cid  小区id
     * @param content  投诉内容
     * @return
     */
    @Override
    public boolean insertSuggest(int uid, int cid, String content) {
        int reuid = communityMapper.findWidById(cid);
        Suggest suggest = new Suggest().setUid(uid).setCid(cid).setContent(content).setReuid(reuid).setSuggestTime(LocalDateTime.now()).setResult("未处理").setStatus(0);
        System.out.println(suggest);
        return suggestMapper.insert(suggest)>0?true:false;
    }


}
